package com.zjf.fupload.config;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.zjf.fupload.FUploadPlugin;
import com.zjf.fupload.entity.ServerInfo;

/**
 * 配置界面
 * @author zjf
 */
public class PreferenceMainPage extends 
	PreferencePage implements IWorkbenchPreferencePage {
	
	/**
	 * The list that displays the current servers
	 */
    private List serverList;

    /**
     * The newEntryText is the text where new bad words are specified
     */
    private Text newEntryText;

	@Override
	protected Control createContents(final Composite parent) {

		Composite entryTable = new Composite(parent, SWT.NULL);

		// Create a data that takes up the extra space in the dialog .
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.grabExcessHorizontalSpace = true;

		entryTable.setLayoutData(data);

		GridLayout layout = new GridLayout();
		entryTable.setLayout(layout);

		// Add in a dummy label for spacing
		new Label(entryTable, SWT.NONE);

		serverList = new List(entryTable, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
		
		serverList.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				newEntryText.setText(serverList.getItem(serverList.getSelectionIndex()));
			}
			
		});

		serverList.setItems(FUploadPlugin.getDefault().getServersPreference());

		// Create a data that takes up the extra space in the dialog and spans
		// both columns.
		data = new GridData(GridData.FILL_BOTH);

		serverList.setLayoutData(data);

		Composite buttonComposite = new Composite(entryTable, SWT.NULL);
		GridLayout buttonLayout = new GridLayout();
		buttonLayout.numColumns = 2;
		buttonComposite.setLayout(buttonLayout);

		// Create a data that takes up the extra space in the dialog and spans
		// both columns.
		data = new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING);

		buttonComposite.setLayoutData(data);

		Button addButton = new Button(buttonComposite, SWT.PUSH | SWT.CENTER);

		addButton.setText("Add to List"); //$NON-NLS-1$
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				String server = newEntryText.getText().replace("：", ":");
				ServerInfo info = ServerInfo.parse(server);
				if(info == null) {
					MessageDialog.openInformation(parent.getShell(), FUploadPlugin.NAME,
						"格式非法，格式必须为 name:server:port:username:port:path");
					return;
				}
				serverList.add(server, serverList
						.getItemCount());
			}
		});

		newEntryText = new Text(buttonComposite, SWT.BORDER);

		// Create a data that takes up the extra space in the dialog .

		data = new GridData(GridData.FILL_HORIZONTAL);

		data.grabExcessHorizontalSpace = true;

		newEntryText.setLayoutData(data);

		Button removeButton = new Button(buttonComposite, SWT.PUSH | SWT.CENTER);

		removeButton.setText("Remove Selection"); //$NON-NLS-1$

		removeButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent event) {
				serverList.remove(serverList.getSelectionIndices());
			}

		});

		data = new GridData();

		data.horizontalSpan = 2;

		removeButton.setLayoutData(data);

		return entryTable;
	}

	@Override
	public void init(IWorkbench workbench) {
		setPreferenceStore(FUploadPlugin.getDefault().getPreferenceStore());
	}
	
	/**
	 * 
	 * Performs special processing when this page's Restore Defaults button has
	 * been pressed.
	 * 
	 * Sets the contents of the nameEntry field to
	 * 
	 * be the default
	 */
	protected void performDefaults() {
		serverList.setItems(FUploadPlugin.getDefault()
				.getDefaultServersPreference());
		
	}
    
    /**

     * Method declared on IPreferencePage. Save the

     * author name to the preference store.

     */
    public boolean performOk() {
    	FUploadPlugin.getDefault().setBadWordsPreference(serverList.getItems());
    	return super.performOk();

    }

}
